<template>
	<view class="page">
		<template v-if="isLogin"> 
			<web-view class="webview" :src="url" :webview-styles="webviewStyles" @message="PostMessage"> 	</web-view>						 
		</template>
	</view>
</template>

<script> 
	import {
		HTTP_REQUEST_URL
	} from '@/config.js'
	export default {
		name: 'name',
		data() {
			return { 
				isLogin: false, 
				userInfo: uni.getStorageSync('userInfo'),
				loginStateUpdate: false,
				app_name: getApp().globalData.app_name, 
				url:'',
				webviewStyles: {
					progress: {
						color: '#FF3333'
					}
				},
				timer:null,
				prevlongitude:'',
				prevlatitude:'',
				longitude:'',
				latitude:'',
				circles: [{
					latitude: '',
					longitude: '',
					color: "#ff00000A",
					fillColor: "#ff00003A",
					radius: "50"
				}],
				markers:[],
				params:{
					pid:'',
					gcj02_lat:'',
					gcj02_lng:'',
					fid:''
				}

			}


		},
		async onLoad(params) {	  
			this.isLogin=await this.$loginState('check');
			// cookiekey = uni.getStorageSync('cookiekey'); 
			this.params.pid=params.pid;
			if(uni.getStorageSync('cy_fid')){
				this.params.fid=uni.getStorageSync('cy_fid');
			}
			console.log('cy_fid',uni.getStorageSync('cy_fid'))
			if(this.isLogin){
				this.getLocal();
			}
			
			
			
			
		},
		async onShow() {
			// 10s获取1次位置
			// this.timer = setInterval(() => {
			// 	this.updateLocus();
			// }, 10000)
		},
		onHide() {
			if(this.timer){
				clearInterval(this.timer);    // 关闭轮询位置定时器
			}
			uni.setStorage({
				key:'cy_fid',
				data:this.params.fid
			}) 
			
		}, 
		onUnload() {
			if(this.timer){
				clearInterval(this.timer);    // 关闭轮询位置定时器
			}
			uni.setStorage({
				key:'cy_fid',
				data:this.params.fid
			})  
		},
		methods: {
			// 获取当前位置
			getLocal: function() {			
				let _this = this; 
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					 highAccuracyExpireTime: 5000,
					success: function(res) {    
						_this.latitude=res.latitude;
						_this.longitude=res.longitude;
						_this.url =`${HTTP_REQUEST_URL}/map/nm?fid=${_this.params.fid}&pid=${_this.params.pid}&lat=${res.latitude}&lng=${res.longitude}&${uni.getStorageSync('cookiekey')}`;  
					},
					fail() {
						 
					}
				});
			},
			
			// 更新轨迹点
			updateLocus: function() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude:true,
					highAccuracyExpireTime:5000,
					accuracy:50,
					success: function(res) {	
							
						
						// console.log('位置变化')
						// _this.latitude = res.latitude;
						// _this.longitude = res.longitude;	
						// _this.uploadLocus();	
						
						//  判断是否相同地址范围 设置位置偏差
						let difx = Math.abs(_this.prevlongitude - res.longitude);
						let dify = Math.abs(_this.prevlatitude - res.latitude);
						console.log('获取位置',res.accuracy,res.speed,difx,dify) 
						
						if(res.accuracy<30 && res.speed>0){
							if ((_this.prevlongitude == '' || _this.prevlatitude == '') || (difx > 0.00005 && difx < 0.0035) || (dify > 0.00005 && dify < 0.0035)) {
								console.log('位置变化')
								_this.latitude = res.latitude;
								_this.longitude = res.longitude;	
								_this.prevlongitude = res.longitude;
								_this.prevlatitude = res.latitude;
								_this.uploadLocus();						    
							} 
						}
										
					}
				});
			},
			
			uploadLocus:async function(){
				let data={};
				if(this.params.fid){
					data={
						pid:this.params.pid,
						gcj02_lat:this.latitude,
						gcj02_lng:this.longitude,
						fid:this.params.fid
					}
				}else{
					data={
						pid:this.params.pid,
						gcj02_lat:this.latitude,
						gcj02_lng:this.longitude 
					}
				}
				let res=await this.$api.pro_api.saveCyArea(data) 
				console.log(res,data)
				if(res.data.ret==0){										
					this.params.fid=res.data.data;
				}
			},
			
			// 更新轨迹点
			PostMessage: function(res) { 
				for(let k in res.detail.data[0]){
					if(k=='start'){
						// 10s获取1次位置 
						uni.hideLoading();
						this.uploadLocus();
						this.timer = setInterval(() => {
							this.updateLocus();
						}, 10000) 
						
					} 
					if(k=='stop'){
						console.log('结束抽样');
						clearInterval(this.timer);
						this.timer=null;
					}
					if(k=='startWork'){
						
					}
				}
			},
			
		},
		components: {

		},

		computed: {

		},
		watch: {

		}

	}
</script>

<style lang="less" scoped> 
.webview {
		width: 100%;
		height: 100%;
	}
</style>
